package com.xuecheng.media;

import io.minio.*;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import org.junit.Test;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * ClassName: testMinio
 * Package: com.xuecheng.media
 * Description:
 *
 * @Author 看到风
 * @Create 2024/8/6 12:55
 * @Version 1.0
 */

public class testMinio {
    static MinioClient minioClient =
            MinioClient.builder()
                    .endpoint("http://8.140.27.245:9000")
                    .credentials("root", "abc123456")
                    .build();
    //上传文件
    @Test
    public void upload() {
        try {
            UploadObjectArgs testbucket = UploadObjectArgs.builder()
                    .bucket("testbucket")
                    // .object("test001.mp4")
                    .object("001/test001.mp4")//添加子目录
                    .filename("E:\\python-learn\\yolov5-master\\runs\\detect\\exp4\\school.mp4")
                    .contentType("video/mp4")//默认根据扩展名确定文件 内容类型，也可以指定
                    .build();
            minioClient.uploadObject(testbucket);
            System.out.println("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
    }

    //将分块文件上传至 minio
    @Test
    public void uploadChunk(){
        String chunkFolderPath = "E:\\game\\chunk\\";
        File chunkFolder = new File(chunkFolderPath);
//分块文件
        File[] files = chunkFolder.listFiles();
//将分块文件上传至 minio
        for (int i = 0; i < files.length; i++) {
            try {
                UploadObjectArgs uploadObjectArgs =
                        UploadObjectArgs.builder().bucket("testbucket").object("chunk/" +
                                i).filename(files[i].getAbsolutePath()).build();
                minioClient.uploadObject(uploadObjectArgs);
                System.out.println("上传分块成功"+i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //合并文件，要求分块文件最小 5M
    @Test
    public void test_merge() throws Exception {
        List<ComposeSource> sources = Stream.iterate(0, i -> ++i)
                .limit(6)
                .map(i -> ComposeSource.builder()
                        .bucket("testbucket")
                        .object("chunk/".concat(Integer.toString(i)))
                        .build())
                .collect(Collectors.toList());
        ComposeObjectArgs composeObjectArgs =
                ComposeObjectArgs.builder().bucket("testbucket").object("merge01.mp4").sources(sources).build();
                        minioClient.composeObject(composeObjectArgs);
    }
    //清除分块文件
    @Test
    public void test_removeObjects(){
//合并分块完成将分块文件清除
        List<DeleteObject> deleteObjects = Stream.iterate(0, i -> ++i)
                .limit(6)
                .map(i -> new
                        DeleteObject("chunk/".concat(Integer.toString(i))))
                .collect(Collectors.toList());
        RemoveObjectsArgs removeObjectsArgs =
                RemoveObjectsArgs.builder().bucket("testbucket").objects(deleteObjects).build();
        Iterable<Result<DeleteError>> results =
                minioClient.removeObjects(removeObjectsArgs);
        results.forEach(r->{
            DeleteError deleteError = null;
            try {
                deleteError = r.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
